package com.base.common.viewmodel;

import androidx.annotation.NonNull;

import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.BaseResponse;
import com.base.common.utils.JavaMethod;
import com.base.common.utils.LogUtil;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Function;

public abstract class NetWorkFunction<T, R> implements Function<T, R> {

    private boolean success = false;
    private String msg = "";

    @Override
    public R apply(@NonNull T t) throws Exception {
        LogUtil.e("NetWorkFunction: apply() " + "--> 开始处理请求得到的结果" );
        if (t instanceof Map)
        {
            LogUtil.e("NetWorkFunction: apply() " + "--> Map" );
            Map map = (Map) t;
            success = JavaMethod.transformClass(map.get("success"), Boolean.class, false);
            msg = JavaMethod.transformClass(map.get("msg"), String.class);
        }
        else if (t instanceof BaseResponse)
        {
            LogUtil.e("NetWorkFunction: apply() " + "--> BaseResponse" );
            int code = ((BaseResponse) t).getCode();
            success = (code==0);
            msg = ((BaseResponse) t).getMsg();
        }
        else if( t instanceof BaseApiResult)
        {
            LogUtil.e("NetWorkFunction: apply() " + "--> BaseApiResult" );
            success = ((BaseApiResult)t).isSuccess();
        }
        else
        {
            success = JavaMethod.getFieldValue(t, "success", Boolean.class);
            msg = JavaMethod.getFieldValue(t, "msg", String.class);
        }

        if (success)
        {
            LogUtil.e("NetWorkFunction: onSuccess() " + "--> t" );
            return onSuccess(t);
        }
        else if ( t == null )
        {
            //BaseResponse.isSuccess(success)
            if (isEmptyData(t)) return onEmptyData(t);
            else
                LogUtil.e("NetWorkFunction: onSuccess() " + "--> t == null" );
                return onSuccess(t);
        }
        else
            LogUtil.e("NetWorkFunction: onFailure() " + "--> t " );
        return onFailure(t);

    }


    public abstract R onSuccess(T t);

    public abstract R onFailure(T t);

    public R onEmptyData(T t)
    {
        LogUtil.e("NetWorkFunction: onEmptyData() " + "--> t" );
        return onFailure(t);
    }

    //判断结果是否为空
    public boolean isEmptyData(T data) {
        return false;
    }


    public <T> T setBeanCode(T t)
    {
        LogUtil.e("NetWorkFunction: setBeanCode() " + "--> t" );
        if (t == null || t instanceof Map) {
            if (t == null) t = (T) new HashMap();
            Map map = (Map) t;
            map.put("code", 99);
            map.put("msg", msg);
        } else if (t instanceof BaseResponse) {
            ((BaseResponse) t).setCode(99);
            ((BaseResponse) t).setMsg(msg);
        }
        else if( t instanceof BaseApiResult)
        {
            LogUtil.e("NetWorkFunction: setBeanCode()" + "--> t" );
            ((BaseApiResult) t).setSuccess(((BaseApiResult) t).isSuccess());
            ((BaseApiResult) t).setCode(((BaseApiResult) t).getCode());
            ((BaseApiResult) t).setMsg(((BaseApiResult) t).getMsg());
        }
        else {
            JavaMethod.setFieldValue(t, "code", 99);
            JavaMethod.setFieldValue(t, "msg", msg);
        }
        return t;
    }

    public <T> Observable<T> getFailureObservable(T t)
    {
        T finalT = setBeanCode(t);
        LogUtil.e("NetWorkFunction: getFailureObservable() " + "--> FailureObservable" );
        return Observable.create(new ObservableOnSubscribe<T>() {
            @Override
            public void subscribe(ObservableEmitter<T> emitter) throws Exception {
                emitter.onNext(finalT);
                emitter.onComplete();
            }
        });

    }


    public <T> Observable<T> getObservable(T t)
    {
        return Observable.create(new ObservableOnSubscribe<T>() {
            @Override
            public void subscribe(ObservableEmitter<T> emitter) throws Exception {
                emitter.onNext(t);
                emitter.onComplete();
            }
        });
    }

}
